package leetcode.problems;

/**
 * _0404 ZigZag Conversion
 * 合并两个有序列表
 * Created by gmwang on 2018/3/23
 */
public class _0405ZigZagConversion {
    /**
     *The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

         P   A   H   N
         A P L S I I G
         Y   I   R
         And then read line by line: "PAHNAPLSIIGYIR"
         Write the code that will take a string and make this conversion given a number of rows:


         字符串“PAYPALISHIRING”一个给定的行数，这样一个曲折的模式：（你可能想在一个固定的字体更加易读显示模式）
         然后逐行读取：“PAHNAPLSIIGYIR”编写将带一个字符串并将给定数行转换的代码：

         string convert(string text, int nRows);
         convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
     */
    /**
     * 思路：
     * 1.
     *
     * @param
     * @return
     */
    public static String convert(String s, int numRows) {
        if(numRows<=1)return s;
        StringBuilder[] sb=new StringBuilder[numRows];
        for(int i=0;i<sb.length;i++){
            sb[i]=new StringBuilder("");   //init every sb element **important step!!!!
        }
        int incre=1;
        int index=0;
        //PAYPALISHIRING
        for(int i=0;i<s.length();i++){
            //s[0]=P,s[1]=A,s[2]=Y
            sb[index].append(s.charAt(i));
            if(index==0){incre=1;}
            if(index==numRows-1){incre=-1;}
            index+=incre;
        }
        String re="";
        for(int i=0;i<sb.length;i++){
            re+=sb[i];
        }
        return re.toString();
    }

    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        System.out.println(convert(s,3));
    }
}
